package com.tin.dao;

import com.tin.bean.TokenInfo;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @author liming_oicq@163.com
 * @date 2018-10-14 15:28
 */
public interface TokenDao extends CrudRepository<TokenInfo,Integer>{

    List<TokenInfo> findByUsername(String username);

    @Transactional
    void deleteByUsername(String username);

    @Transactional
    @Modifying
    @Query(value = "INSERT INTO token_info( username, token, ip, expire_Date ) VALUES(?1, ?2, ?3, ?4)", nativeQuery = true)
    Integer insertTokenInfo(String username, String token, String ip, String expireDate);

    @Transactional
    @Modifying
    @Query(value = "DELETE FROM token_info WHERE  token = ?1", nativeQuery = true)
    void deleteByToken(String token);

    @Transactional
    @Modifying
    @Query(value = "UPDATE token_info SET expire_Date = ?1 WHERE  token = ?2", nativeQuery = true)
    void updateExpireDateByToken(String expireDate, String token);
}
